package com.ztjc.day02_storage.util;

import android.os.Environment;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by pro on 15/10/7.
 */
public class ExternalStorageUtil {

    /**
     * 保存账户信息
     *
     * @param account
     * @param pass
     * @return
     */
    public static boolean saveAccount(String account, String pass) {
        if (isSDCanUse()) {
            FileOutputStream outputStream = null;
            try {
                outputStream = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), "/account.txt"));
                outputStream.write((account + "##" + pass).getBytes());
                return true;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (outputStream != null) {
                    try {
                        outputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return false;
    }

    /**
     * 判断sd卡是否可用
     * @return
     */
    private static boolean isSDCanUse() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    /**
     * 获取账户信息
     *
     * @return
     */
    public static Map<String, String> getAccount() {
        Map<String, String> result = null;
        if (isSDCanUse()) {
            FileInputStream inputStream = null;
            BufferedReader reader = null;
            InputStreamReader inputStreamReader = null;
            try {
                inputStream = new FileInputStream(new File(Environment.getExternalStorageDirectory(), "/account.txt"));
                inputStreamReader = new InputStreamReader(inputStream);
                reader = new BufferedReader(inputStreamReader);
                result = new HashMap<>();
                String s = reader.readLine();
                String[] split = s.split("##");
                result.put("account", split[0]);
                result.put("pass", split[1]);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (inputStreamReader != null) {
                    try {
                        inputStreamReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return result;
    }
}
